﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tenebrous_Manor.Randomization
{
    class RandomResearchNotes
    {
        public static ResearchNote[] research_notes = new ResearchNote[6];

        public static List<Navigation.Room> possible_room;

        static Random randomNumbers = new Random((int)DateTime.Now.Ticks);
        public static void Initialize()
        {
            possible_room = new List<Navigation.Room> { Navigation.Room.JUNK_ROOM, Navigation.Room.BEDROOM, Navigation.Room.KITCHEN, Navigation.Room.STUDY, Navigation.Room.FOYER, Navigation.Room.LOWER_LANDING };

            List<Dialogue.Dialogue_keys> note = new List<Dialogue.Dialogue_keys> 
            {
                Dialogue.Dialogue_keys.RESEARCH_NOTE1,
                Dialogue.Dialogue_keys.RESEARCH_NOTE2,
                Dialogue.Dialogue_keys.RESEARCH_NOTE3,
                Dialogue.Dialogue_keys.RESEARCH_NOTE4,
                Dialogue.Dialogue_keys.RESEARCH_NOTE5,
                Dialogue.Dialogue_keys.RESEARCH_NOTE6
            };

            for (int i = 0; i <= research_notes.Length - 1; i++)
            {
                int index = randomNumbers.Next(possible_room.Count);

                Tuple<int, int> temp = Tuple.Create(0,0);
                switch (possible_room[index])
                {
                    case Navigation.Room.JUNK_ROOM:
                        temp = JunkRoomLocation();
                        break;
                    case Navigation.Room.BEDROOM:
                        temp = BedRoomLocation();
                        break;
                    case Navigation.Room.KITCHEN:
                        temp = KitchenLocation();
                        break;
                    case Navigation.Room.STUDY:
                        temp = StudyLocation();
                        break;
                    case Navigation.Room.FOYER:
                        temp = FoyerLocation();
                        break;
                    case Navigation.Room.LOWER_LANDING:
                        temp = LowerLandingLocation();
                        break;
                }

                research_notes[i] = new ResearchNote(note[i], possible_room[index], temp);

                possible_room.RemoveAt(index);
            }
        }

        public static bool foundAllNotes()
        {
            return research_notes[0].isFound() && research_notes[1].isFound() 
                && research_notes[2].isFound() && research_notes[3].isFound() 
                && research_notes[4].isFound() && research_notes[5].isFound(); 
        }

        public static String getFoundNotes()
        {
            String result = "";

            foreach (ResearchNote note in research_notes)
            {
                if (note.isFound())
                {
                    result += Dialogue.dialogue[note.getKey()] + "\n";
                }
            }

            if (result.Equals(""))
                result = "All the pages seem to have been ripped out...";
            return result;
        }

        //Generate note anywhere in junk
        private static Tuple<int, int> JunkRoomLocation()
        {
            return Tuple.Create(randomNumbers.Next(2, 8), randomNumbers.Next(2, 8)); 
        }

        //Generate note in 1 of the drawers
        private static Tuple<int, int> BedRoomLocation()
        {
            Tuple<int, int> temp;
            List<int> x = new List<int> { 1, 2, 7, 8 };
            //generate again if key is in the same location.
            do
            {
                temp = Tuple.Create(x[randomNumbers.Next(x.Count)], 1);
            } while (temp == RandomizedClueLocations.studyKey);

            return temp;
        }

        private static Tuple<int, int> KitchenLocation()
        {
            Tuple<int, int> temp;
            do
            {
                temp = GameConstants.possibleLeverLocation[randomNumbers.Next(GameConstants.possibleLeverLocation.Count)];
            } while (temp == RandomizedClueLocations.lever);

            return temp;
        }

        private static Tuple<int, int> StudyLocation()
        {
            Tuple<int, int> temp;
            do
            {
                temp = GameConstants.possibleStudyNoteLocation[randomNumbers.Next(GameConstants.possibleStudyNoteLocation.Count)];
            } while (temp == RandomizedClueLocations.studyNote);

            return temp;
        }

        private static Tuple<int, int> FoyerLocation()
        {
            return Tuple.Create(randomNumbers.Next(GameConstants.ROW_COUNT), randomNumbers.Next(GameConstants.COL_COUNT)); 
        }

        private static Tuple<int, int> LowerLandingLocation()
        {
            List<int> y = new List<int> { 2, 3, 6, 7 };

            return Tuple.Create(randomNumbers.Next(2, 8), y[randomNumbers.Next(y.Count)]); 
        }
    }
}
